package org.create.builder;

import org.create.bean.FieldInfo;
import org.create.bean.TableInfo;
import org.create.utils.DataTool;
import org.create.utils.DateTool;
import org.create.utils.IoTool;
import org.create.utils.LogTool;

import java.io.BufferedWriter;
import java.io.File;
import java.io.IOException;
import java.util.Date;
import java.util.List;
import java.util.Map;

import static org.create.bean.Constants.*;

public class BuildService {
    private static BufferedWriter bw;

    private static void buildPackageComment(TableInfo tableInfo) throws IOException {
        writer("package " + PACKAGE_BASE + "." + PACKAGE_SERVICE + ";", 2);
        writer("import " + PACKAGE_BASE + "." + PACKAGE_PO + "." + tableInfo.getBeanName() + ";");
        writer("import " + PACKAGE_BASE + "." + PACKAGE_QUERY + "." + tableInfo.getBeanName() + "Query;");
        writer("import " + PACKAGE_BASE + "." + PACKAGE_VO + ".PaginationResultVo;", 2);
        writer("import java.util.List;", 3);
    }

    private static void buildDescriptionComment(TableInfo tableInfo) throws IOException {
        writer("/**");
        writer(" * Description " + tableInfo.getComment() + "业务接口");
        writer(" * date " + DateTool.DateFormat(new Date(), YMD_HMS));
        writer(" */");
    }

    private static void buildClassComment(TableInfo tableInfo) throws IOException {
        writer("public interface " + tableInfo.getBeanName() + "Service {");
        buildFieldComment(tableInfo);
        writer("}");
    }

    private static void buildFieldComment(TableInfo tableInfo) throws IOException {
        String beanName = tableInfo.getBeanName();
        buildFieldDescriptionComment("根据条件查询列表");
        writer("    List<" + beanName + "> findListByParam(" + beanName + "Query query);", 2);
        buildFieldDescriptionComment("根据条件查询数量");
        writer("    Integer findCountByParam(" + beanName + "Query query);", 2);
        buildFieldDescriptionComment("分页查询");
        writer("    PaginationResultVo<" + beanName + "> findListByPage(" + beanName + "Query query);", 2);
        buildFieldDescriptionComment("新增");
        writer("    Integer add(" + beanName + " bean);", 2);
        buildFieldDescriptionComment("新增或修改");
        writer("    Integer addOrUpdate(" + beanName + " bean);", 2);
        buildFieldDescriptionComment("批量新增");
        writer("    Integer addBatch(List<" + beanName + "> listBean);", 2);
        buildFieldDescriptionComment("批量新增或修改");
        writer("    Integer addOrUpdateBatch(List<" + beanName + "> listBean);", 2);

        for (Map.Entry<String, List<FieldInfo>> entry : tableInfo.getKeyIndexMap().entrySet()) {
            List<FieldInfo> fieldInfoList = entry.getValue();
            StringBuilder sb = new StringBuilder();
            StringBuilder params = new StringBuilder();
            int firstLoop = 1;
            for (FieldInfo fieldInfo : fieldInfoList) {
                sb.append(DataTool.firstChatToUpper(fieldInfo.getPropertyName()));
                params.append(fieldInfo.getJavaType()).append(" ").append(fieldInfo.getPropertyName());
                if (firstLoop < fieldInfoList.size()) {
                    sb.append("And");
                    params.append(", ");
                    firstLoop++;
                }
            }
            buildFieldDescriptionComment("根据" + sb + "查询对象");
            writer("    " + beanName + " get" + DataTool.firstChatToUpper(beanName) + "By" + sb + "(" + params + ");", 2);
            buildFieldDescriptionComment("根据" + sb + "修改");
            writer("    Integer update" + DataTool.firstChatToUpper(beanName) + "By" + sb + "(" + beanName + " bean, " + params + ");", 2);
            buildFieldDescriptionComment("根据" + sb + "删除");
            writer("    Integer delete" + DataTool.firstChatToUpper(beanName) + "By" + sb + "(" + params + ");", 2);
        }
    }

    private static void buildFieldDescriptionComment(String description) throws IOException {
        writer("    /**");
        writer("     * " + description);
        writer("     */");
    }

    private static void writer(String str) throws IOException {
        writer(str, 1);
    }

    private static void writer(String str, int num) throws IOException {
        bw.write(str);
        if (num > 0) {
            appendLine(num);
        }
    }

    private static void appendLine() throws IOException {
        appendLine(1);
    }

    private static void appendLine(int loop) throws IOException {
        if (loop == 1) {
            bw.newLine();
        } else {
            for (int i = 0; i < loop; i++) {
                bw.newLine();
            }
        }
    }

    public static void run(List<TableInfo> tableInfoList) {
        for (TableInfo tableInfo : tableInfoList) {
            File file = new File(PATH_SERVICE + tableInfo.getBeanName() + "Service" + JAVA_SUFFIX);
            File dir = file.getParentFile();
            boolean d = dir.mkdirs();
            LogTool.printLog(d, "新建目录", dir);
            try {
                boolean r = file.createNewFile();
                LogTool.printLog(r, "新建文件", file);
                bw = IoTool.createBw(file);
                buildPackageComment(tableInfo);
                buildDescriptionComment(tableInfo);
                buildClassComment(tableInfo);
            } catch (Exception e) {
                LogTool.printError(e);
            } finally {
                IoTool.close();
            }
        }
    }
}
